import XEUtils from 'xe-utils'
import utils from "@common/utils/utils";
import common from "@common/api/common";
import poiApi from "@common/api/poiApi";
import constants from "@common/api/constants";
import GlobalDialog from "@common/components/dialog/global";
import DynamicForm from "@common/components/form/Dynamic";
import { genFormConfig } from "@common/components/form/Dynamic/util";

const genButtons = (exportOpt, store) => {
	let buttons = [];
	buttons.push({
		text: '关 闭',
		click(dialog) {
			dialog.visible = false;
		},
	});
	buttons.push({
		type: 'primary',
		text: '确 定',
		click(dialog, { component }) {
			component.validateDynamicForm().then(valid => {
				if (valid) {
					return saveSetting(component, exportOpt.dialogOpt, store);
				}
				return valid;
			}).then(valid => {
				if (valid) {
					dialog.visible = false;
					doExport(exportOpt);
				}
			});
		}
	});
	return buttons;
};
const saveSetting = (component, dialogOpt, store) => {
	let subData = $.extend({}, dialogOpt.user_setting, {
		code: store.state.userInfo.sid,
		json: JSON.stringify($.extend({}, component.formData)),
	});
	delete subData.json_object;
	return common.requestData(constants.API_SaveSysSetting, subData).then(() => {
		store.dispatch('initUserSetting', subData.code);
		return true;
	}).catch(() => {
		return false;
	});
}

// 获取函数的参数名
const getParameterName = fn => {
	if (typeof fn !== 'object' && typeof fn !== 'function') return;
	const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
	const DEFAULT_PARAMS = /=[^,)]+/mg;
	const FAT_ARROWS = /=>.*$/mg;
	let code = fn.prototype ? fn.prototype.constructor.toString() : fn.toString();
	code = code.replace(COMMENTS, '').replace(FAT_ARROWS, '').replace(DEFAULT_PARAMS, '');
	let result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g);
	return result === null ? [] : result;
};

const doExport = exportOpt => {
	let exportFn = poiApi[exportOpt.method];
	if (typeof exportFn !== 'function') {
		return;
	}
	let paramNameList = getParameterName(exportFn);
	if (!utils.isNotEmptyList(paramNameList)) {
		return;
	}
	if (paramNameList.length === 1) {
		exportFn(exportOpt.params);
		return;
	}
	let paramsList = [];
	paramNameList.forEach(paramsName => {
		paramsList.push(exportOpt.params[paramsName]);
	});
	exportFn.apply(null, paramsList);
};
const ExportForm = (store, router) => {
	let dialog = GlobalDialog(store, router);
	return async exportOpt => {
		if (exportOpt.dialogOpt) {
			let dialogOpt = $.extend({}, exportOpt.dialogOpt);
			let data = {};
			if (store.state.user_setting) {
				data = $.extend({}, (store.state.user_setting.json_object || {}), {
					the_date: XEUtils.toDateString(new Date(), 'yyyy-MM-dd'),
				});
			}
			return dialog({
				title: dialogOpt.title || '导出',
				width: '600px',
				component: {
					value: DynamicForm,
					props: {
						formConfig: genFormConfig(dialogOpt.formConfig),
						data,
						formEvent: dialogOpt.formEvent,
					},
				},
				buttons: genButtons(exportOpt, store),
			});
		}
		return doExport(exportOpt);
	};
};
export default ExportForm;